package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("cookie")
public class CookieController {

    @RequestMapping("getCookie")
    public String  getCookie(HttpServletResponse response){
        //创建cookie对象，并添加cookie数据
        Cookie cookie = new Cookie("myname","123");
        cookie.setMaxAge(20000);
        //将 cookie信息发送给客户端
        response.addCookie(cookie);
        response.addCookie(new Cookie("aaa","111"));

        return "ok";
    }

    @RequestMapping("printCookie")
    public String  printCookie(HttpServletRequest request){
        //得到请求头中所有的cookie
        Cookie[] cookieArray = request.getCookies();
        String str = "cookie信息：";
        for(Cookie c : cookieArray){
            str += c.getName()+"    "+c.getValue()+"<br>";
        }

        return str;
    }
}
